COMPILER=g++
LINKER=g++
OPTS =-c
PROTO_DIR = $(CURDIR)/../../proto
INCLUDE_DIR = $(CURDIR)/../../include
INCLUDE_HEADER = -I$(CURDIR)/../../include/ -I$(CURDIR)/../../proto/
BINARY = $(CURDIR)/../../bin/slave
LIB = $(CURDIR)/../../lib

PROTOBUFLIB = `pkg-config --cflags --libs protobuf` -Wl,-rpath,$(LIB)
LDFLAGS = -Wall -g -O0 $(PROTOBUFLIB)
PROTOC = ../../bin/protoc

OBJS = slave.o socket.o $(PROTO_DIR)/load.pb.o 

all: $(BINARY) scripts

$(BINARY) : $(OBJS)
	pkg-config --cflags --libs protobuf
	$(LINKER) $(LDFLAGS) $(OBJS) -o $(BINARY)

$(PROTO_DIR)/load_proto : $(PROTO_DIR)/load.proto
	$(PROTOC) -I=$(PROTO_DIR) --cpp_out=$(PROTO_DIR) $(PROTO_DIR)/load.proto
	pkg-config --cflags --libs protobuf
	$(COMPILER) $(OPTS) $(PROTO_DIR)/load.pb.cc $(LDFLAGS) -o $(PROTO_DIR)/load.pb.o 
	@touch $(PROTO_DIR)/load_proto
	
slave.o : $(INCLUDE_DIR)/socket.h $(PROTO_DIR)/load_proto slave.cpp 
	$(COMPILER) $(OPTS) $(PROTO_DIR)/load.pb.cc slave.cpp $(INCLUDE_HEADER)

socket.o : $(INCLUDE_DIR)/socket.h ../socket.cpp
	$(COMPILER) $(OPTS) ../socket.cpp $(INCLUDE_HEADER)


	
clean :
	rm -rf *.o $(BINARY) $(PROTO_DIR)/load_proto

scripts:
	chmod +x *.sh
